<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DingdanController extends Controller
{   

    public function getDingdan(Request $request)
    {
        $did = DB::table('gonggao')->get();
        $dif = DB::table('link')->get();
        // dd('111');
    	// 读取数据 并且分页
      // dd(session('id'));
      $data['user_id'] = session('id');
      // session(['id'=>$user->id]);
      // dd($data);
      $dat = [];
      $res = DB::table('orders')->where('user_id',$data['user_id'])->get();
      // dd($res);
      foreach ($res as $k=>$v){
      
        $tmp['id'] = $v->id;
        $tmp['order_code'] = $v->order_code;
        $tmp['created_at'] = $v->created_at;
        $tmp['total'] = $v->total;
        $tmp['status'] = $v->status;
        $dat[]=$tmp;
    }
        // dd($dat);
      // $res = json_encode($res);
      // $res = json_decode($res);
    	 // $orders=DB::table('orders')
      //    ->where(function($query)use($request){
      //       $query->where('user_id','like','%'.$request->input('keywords').'%');
      //   })
      //   ->paginate($request->input('num',10));
      // dd($orders);
        //解析模板
        return view('geren.dingdan',['dat'=>$dat,'aaa'=>$did],['lianjie'=>$dif]);  
    }

    public function delete(Request $request){

      $id=$request->input('id');
      // dd($id);
      $num = DB::table('orders')->where('id',$id)->delete();
      echo $num;
      // echo ;
    }
    public function getXiangq(Request $request){
      // dd('helo');
      // echo 11;
      // dd($request->all());
      $id=$request->input('id');
      // dd($id);
      $res=DB::table('orders')->where('id',$id)->first();
        $data = [];
        $tmp['id'] = $res->id;
        // dd($data['id']);
        $tmp['address_id'] = $res->address_id;
        $tmp['order_code'] = $res->order_code;
        $der = DB::table('order_goods')->where('order_id',$tmp['id'])->get();
        // dd($der);
        foreach ($der as $k=>$v)
        {
            $tmp['goods_id'] = $v->goods_id;
            $der2 = DB::table('goods')->where('id',$tmp['goods_id'])->first();
            $tmp['num'] = $v->num;
            $tmp['proname'] = $der2->proname;
            $tmp['price'] = $der2->price;
            $tmp['xiaoji'] = $tmp['price']*$tmp['num'];
            $der1 = DB::table('addresses')->where('id',$tmp['address_id'])->first();
            $tmp['total'] = $res->total;
            $tmp['phone'] = $der1->phone;
            $data[] = $tmp;
            
        }
        // dd($data);
        return view('geren.xiangq',['data'=>$data]);
    }

}
